home *** CD-ROM | disk | FTP | other *** search
- Path: ns2.mainstreet.net!ablecom!usenet
- From: The Letter O <jczech@ablecom.net>
- Newsgroups: comp.lang.c++
- Subject: easy c++ question
- Date: Mon, 08 Apr 1996 05:08:58 -0700
- Organization: HERNK.com
- Message-ID: <316901DA.3138C677@ablecom.net>
- NNTP-Posting-Host: jczech.ppp.ablecom.net
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i586)
-
- Okay, I'm a newbie in C++, and am currently working through
- Practical C++ programming by O'Reilly & Associates. I'm working
- on my first C++ program with classes, and the objective is to
- make a simple stack class with member functions, a constructor
- and a destructor. simple right?
-
- //======================================================
- class stack {
- private:
- int count; // number of items in the stack
- int data[100]; // the items themselves
- public:
- .
- .
- .
- //======================================================
-
-
- I was using the following init() member function to initialize
- my stack variables:
-
- //=======================================
- inline void stack::init(void)
- {
- count = 0; // zero the stack
- }
- //=======================================
-
- Simple, right? right. Then I modified it and made it into a
- constructor, so that it would automatically be called whenever
- I declared a variable of type: stack as follows:
-
- //==============================================
- inline stack::stack(void)
- {
- count = 0; // zero the stack
- cout << "constructor has been called\n";
- }
- //==============================================
-
- When I changed it to the constructor (above) it was not called
- automatically when I declared a variable of type: stack, and
- thus the stack counter wasn't initialized, and I got a SIGSEGV
- signal because count starts out with a junk value that's beyond
- the bounds of the data array of my stack variable.
-
- My point is: Aren't constructors automagically called when a variable
- is declared, simply because they have the same name as the class?
- ie: stack::stack(void)
-
-
- I'm using G++ 2.7.0 under Linux slackware 3.0, and Kernel 1.2.13
- Is there some compiler switch to enable constructors? Is there a bug in
- this version of g++? Am I just a moron who should be banished from programming?
-
- Thanks for any help provided...
-
- *Jczech@ablecom.net
-
- Replies by email and/or newsgroup are appreciated.
-
-